package com.sh.哈希;


import java.util.*;
import java.util.stream.Collectors;

/**
 * @Auther: sh
 * @Version 1.0
 * @Description:
 */
public class 字母异位词分组 {
    public static void main(String[] args) {
        String[] strs = new String[]{"eat","tea","tan","ate","nat","bat"};
        List<List<String>> extracted = extracted(strs);
        System.out.println(extracted);

    }

    private static  List<List<String>>   extracted(String[] strs) {
        HashMap<String, List<String>> hashMap = new HashMap<>();
        for (int i = 0; i < strs.length; i++) {
            char[] charArray = strs[i].toCharArray();
            Arrays.sort(charArray);
            String temp = String.valueOf(charArray);
            if(hashMap.containsKey(temp)){
                List<String> strings = hashMap.get(temp);
                strings.add(strs[i]);
            }else {
                ArrayList<String> list = new ArrayList<>();
                list.add(strs[i]);
                hashMap.put(temp,list);
            }
        }
        List<List<String>> result = new ArrayList<>();
        for (List<String> value : hashMap.values()) {
            System.out.println(value);
            result.add(value);

        }
        return result;
    }
}

